﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace SplenetiX
{
    class MenuPause
    {
        bool isPaused = false;
        float timer = 0f;


        string clickedLink;

        bool isbacktomainmenu;
        Cursor cursorPause;
        Background Window;
        KeyboardState CurrentKeyState;
        List<MenuPauseButton> listButton = new List<MenuPauseButton>();
        MenuPauseButton BackToMenu;

        public MenuPause()
        {
            clickedLink = "None";
            isBackToMainMenu = true;
        }

        public bool Paused
        {
            get { return isPaused; }
            set { this.isPaused = value; }
        }

        public string ClickedLink
        {
            get { return clickedLink; }
            set { this.clickedLink = value; }
        }
        public bool isBackToMainMenu
        {
            get { return isbacktomainmenu; }
            set { this.isbacktomainmenu = value; }
        }



        public void Load(ContentManager content)
        {
            Window = new Background(new Vector2(0, 0));
            Window.LoadContent(content, "Menu_Interface//Menu_Pause//pause");

            cursorPause = new Cursor();
            cursorPause.LoadCursor(content);

            BackToMenu = new MenuPauseButton("BackToMenu", new Vector2(400, 487));

            listButton.Add(BackToMenu);

            foreach (MenuPauseButton b in listButton)
            {
                b.Load(content, "Menu_Interface//Menu_Pause//" + b.Name);
            }
        }

        public void Update(GameTime gameTime, GraphicsDevice graphics, ContentManager content, bool returnInMenu, Menu menu, MenuSolo menusolo)
        {

            CurrentKeyState = Keyboard.GetState();

            timer += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (timer > 0.2f)
            {
                if (CurrentKeyState.IsKeyDown(Keys.Escape))
                {
                    if (isPaused == false)
                        isPaused = true;

                    else
                        isPaused = false;

                    timer = 0f;
                }
            }

            foreach (MenuPauseButton b in listButton)
            {
                b.Update(gameTime, content);

                if (b.IsMouseOver)
                {
                    b.Load(content, "Menu_Interface//Menu_Pause//" + b.Name + "_over");
                    cursorPause.UpdateCursor(graphics, true);
                }
                else
                {
                    b.Load(content, "Menu_Interface//Menu_Pause//" + b.Name);
                    cursorPause.UpdateCursor(graphics, false);
                }

                if (b.IsClicked && returnInMenu)
                {
                    clickedLink = b.Name;
                    if (clickedLink == "BackToMenu")
                    {
                        isbacktomainmenu = true;
                        isPaused = false;
                        menu.Reset();
                        menusolo.isbacktomainmenu = true;
                        menusolo.clickedLink = "None";
                        clickedLink = "None";
                    }
                }
                else clickedLink = "None";
            }

            if (!returnInMenu)
                isbacktomainmenu = false;

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Window.DrawBackground(spriteBatch);
            foreach (MenuPauseButton b in listButton)
            {
                b.Draw(spriteBatch);
            }
            cursorPause.DrawCursor(spriteBatch);
        }

    }

    class MenuPauseButton
    {
        string NameButton;
        Vector2 Pos;
        Texture2D texture_button;
        MouseState state;
        bool isMouseOver, isClicked;
        float timer = 0f;

        public MenuPauseButton(string name, Vector2 position)
        {
            NameButton = name;
            Pos = position;

        }

        public string Name
        {
            get { return NameButton; }
            set { this.NameButton = value; }
        }

        public bool IsMouseOver
        {
            get { return isMouseOver; }
            set { this.isMouseOver = value; }
        }

        public bool IsClicked
        {
            get { return isClicked; }
            set { this.isClicked = value; }
        }

        public Vector2 Position
        {
            get { return Pos; }
            set { this.Pos = value; }
        }

        public void Load(ContentManager contentManager, string assetName)
        {
            texture_button = contentManager.Load<Texture2D>(assetName);
        }

        public void Update(GameTime gameTime, ContentManager content)
        {
            state = new MouseState();

            state = Mouse.GetState();

            timer += (float)gameTime.ElapsedGameTime.TotalSeconds;



            if (state.X >= Position.X && state.X <= Position.X + texture_button.Width && state.Y >= Position.Y &&
                state.Y <= Position.Y + texture_button.Height)
                isMouseOver = true;

            else isMouseOver = false;


            if (isMouseOver && state.LeftButton == ButtonState.Pressed)
                isClicked = true;

            else isClicked = false;





        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture_button, Position, Color.White);
        }



    }
}
